package cs131.pa1.filter.sequential;

import cs131.pa1.filter.Message;

import java.io.*;
import java.util.List;

/**
 * Created by xi.w on 2017-09-26.
 */
public class RedirectionCommand extends SequentialFilter {

    public final static String COMMAND = ">";

    private String para;

    public RedirectionCommand(List<String> commands) {
        para = commands.get(0);
    }

    @Override
    public void process() {
        String path;
        if (para.startsWith(FILE_SEPARATOR)) {
            path = para;
        } else {
            path = SequentialREPL.currentWorkingDirectory + FILE_SEPARATOR + para;
        }

        File file = new File(path);
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            while (!input.isEmpty()) {
                out.write(input.poll() + "\n");
            }
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (next != null) {
            System.out.print(Message.CANNOT_HAVE_OUTPUT.with_parameter(COMMAND + " " + para));
            return;
        }

    }

    @Override
    protected String processLine(String line) {
        return null;
    }
}
